package _2022.hot100._339_除法求值;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： YHSimon
 * @date： 2022-06-01 10:24
 */
public class Solution {
    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        int equationSize = equations.size();

        UnionFind unionFind=new UnionFind(2 * equationSize);
        // // 第 1 步：预处理，将变量的值与 id 进行映射，使得并查集的底层使用数组实现，方便编码
        Map<String, Integer> hashMap = new HashMap<>(2 * equationSize);
        int id=0;
        for(int i=0;i<equationSize;i++){
            List<String> equation= equations.get(i);
            String var1=equation.get(0);
            String var2=equation.get(1);
            if(! hashMap.containsKey(var1)){
                hashMap.put(var1,id);
                id++;
            }
            if(! hashMap.containsKey(var2)){
                hashMap.put(var2,id);
                id++;
            }
            unionFind.union(hashMap.get(var1), hashMap.get(var2),values[i]);
        }

        // 第2步：做查询
        int queriesSize=queries.size();
        double[] res=new double[queriesSize];
        for(int i=0;i<queriesSize;i++){
            String var1=queries.get(i).get(0);
            String var2=queries.get(i).get(1);

            Integer id1=hashMap.get(var1);
            Integer id2=hashMap.get(var2);

            if(id1==null||id2==null){
                res[i]=-1.0d;
            }else{
                res[i]=unionFind.isConnected(id1, id2);
            }
        }
        return res;
    }

    private class UnionFind {
        private int[] parent;
        // 指向父节点的权值
        private double[] weights;

        public UnionFind(int n) {
            this.parent = new int[n];
            this.weights = new double[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
                weights[i] = 1.0d;
            }
        }

        public void union(int x, int y, double value) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) {
                return;
            }
            parent[rootX] = rootY;
            weights[rootX] = weights[y] * value / weights[x];
        }

        //路径压缩
        public int find(int x) {
            if (x != parent[x]) {
                int origin = parent[x];
                parent[x] = find(parent[x]);
                weights[x] *= weights[origin];
            }
            return parent[x];
        }

        public double isConnected(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) {
                return weights[x] / weights[y];
            } else {
                return -1.0d;
            }
        }
    }
}
